package ejbPart;

import ejbPart.entity.bankAccount.*;

import java.util.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

public class Test {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        props.put(Context.PROVIDER_URL, "remote://127.0.0.1:4447");
        props.put(InitialContext.SECURITY_PRINCIPAL, "user");
        props.put(InitialContext.SECURITY_CREDENTIALS, "pass");
        props.put("jboss.naming.client.ejb.context", true);
        //props.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
        Account account = null;
        try {
            Context ctx = new InitialContext(props);
            final String path = "/ejbPart/Account!ejbPart.entity.bankAccount.AccountHome";
            Object obj = ctx.lookup(path);
            AccountHome home = (AccountHome) PortableRemoteObject.narrow(obj, AccountHome.class);

            /* Use factory (home object) for crete the Account EJB Object*/
            //home.create("123-456-7890", "John Smith");

            //Find an account with given name
            /*Iterator iter = home.findByOwnerName("John Smith").iterator();
            if (iter.hasNext()) {
                account = (Account) PortableRemoteObject.narrow(iter.next(), Account.class);
            } else {
                throw new Exception("Can't find record in database");
            }
            System.out.println("Initial balance = " + account.getBalance());
            account.deposite(100);
            System.out.println("Total balance in the bank = " + home.getTotalBankValue());*/

            AccountPK pk = new AccountPK("123-456-7890");
            account = home.findByPrimaryKey(pk);
            account.deposite(100);
            System.out.println("account " + pk.accountId + " balance = " + account.getBalance());

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
